function gameDropDown(x,y,z) {
	$('.dropdowncentermenu').val('#/load/'+x+'/'+y);
	$('.dropdowncentermenu2 option').remove();
	var games = pageMap[y];
	$.each(games, function(val, text) {
		if (val != '') $('.dropdowncentermenu2').append( new Option(val, '#/load/' + x +'/'+ y +'/'+text) );
	});
	$('.dropdowncentermenu2').val('#/load/' + x +'/'+ y +'/'+z);
}

function resetMobileAll() {
	$(".betlist, .betlist-single, .betselection, .betselection-link").removeClass ("betlistactive");
	$(".betarrow, .betselection .betarrow, .betlist-radio .betarrow-radio").css ("display", "none");
	$(".betlist-radio").removeClass ("betlistactive-radio");
	$(".bet_disabled").removeClass("bet_disabled");
	$('.closebtn').trigger('click');
}
	
function resetMoilePartial (btnlocation) {
	var activebet = $(btnlocation).children ().find(".betlistactive");
	$(activebet).removeClass ("betlistactive");
	var activearrow = $(activebet).find(".betarrow");
	$(activearrow).css ("display", "none");
}

function resultsingle(x) {
	window.hasBack = window.location.href;
	location.href = '#/dresult/' + x +'/'+$('#date').val();
}

$(document).ready(function() {
	
	FastClick.attach(document.body);

	$(".toggleonoff").click (function () { 
		var togglestatus = $(this).parent().parent().parent().next(".bet_content").css ("display");
		if (togglestatus == "block") {
			$(this).parent().parent().parent().next(".bet_content").css ("display" , "none");
			$(this).children().html ('<i class="fa fa-plus-square"></i>');
		} else {
			$(this).parent().parent().parent().next(".bet_content").css ("display" , "block");
			$(this).children().html ('<i class="fa fa-minus-square"></i>');
		}
	});

	$(".betlist").click (function () {
		var togglebet = $(this).find(".betarrow").css ("display");
		var enablefunction = $(this).find("p").html ();
		if (enablefunction != "--") {
			if (togglebet == "none") {
				$(this).toggleClass( "betlistactive");
				$(this).find(".betarrow").css ("display" , "block");
			} else {
				$(this).toggleClass( "betlistactive");
				$(this).find(".betarrow").css ("display" , "none");
			}
		}
	});

	$(".betlist-single").click (function () {
		var togglebet = $(this).find(".betarrow").css ("display");
		var enablefunction = $(this).find("p").html ();
		if (enablefunction != "--") {
			if (togglebet == "none") {
				$(this).toggleClass( "betlistactive");
				$(this).find(".betarrow").css ("display" , "block");
			} else {
				$(this).toggleClass( "betlistactive");
				$(this).find(".betarrow").css ("display" , "none");
			}
		}
		$('.submitticket').click();
	});

	$(".betselection").click (function () {
		var togglebet = $(this).find(".betarrow").css ("display");
		var enablefunction = $(".betselection.betlistactive").length;
		var limiter = ($(".betlist-radio.betlistactive-radio .odds").attr('value')) ? ($(".betlist-radio.betlistactive-radio .odds").attr('value')).split(',')[1] : 99;

		if (togglebet == "none" && enablefunction < limiter) {
			$(this).toggleClass( "betlistactive");
			$(this).find(".betarrow").css ("display" , "block");
			if (enablefunction == limiter-1) {
				$(".betselection").not(".betlistactive").addClass("bet_disabled");
			}
		} else if (togglebet != "none") {
			$(this).removeClass( "betlistactive");
			$(this).find(".betarrow").css ("display" , "none");
			$(".bet_disabled").removeClass("bet_disabled");
		}

		if ($(this).parents('.betcombo').length > 0) {
			var oarray_min = $("#empOdds").attr('value').split(',')[0];
			var oarray_max = $("#empOdds").attr('value').split(',')[1];
			var selectedSize = $(".betcombo .betselection.betlistactive").size();

			if (selectedSize >= oarray_min && selectedSize <= oarray_max) {
				$(".betlist-radio-static .oddsArr").removeClass("odds").hide();
				$(".oddsArr"+selectedSize).addClass("odds").show();
			} else {
				$(".betlist-radio-static .oddsArr").removeClass("odds").hide();
				$("#empOdds").addClass("odds").show();
			}
		}
	
	});

	// $(".betcombo .betselection").click (function () {
	// 	var oarray_min = $("#empOdds").attr('oarray').split(',')[0];
	// 	var oarray_max = $("#empOdds").attr('oarray').split(',')[1];
	// 	var selectedSize = $(".betcombo .betselection.betlistactive").size();

	// 	if (selectedSize >= oarray_min && selectedSize <= oarray_max) {
	// 		$(".betlist-radio-static .oddsArr").removeClass("odds").hide();
	// 		$(".oddsArr"+selectedSize).addClass("odds").show();
	// 	} else {
	// 		$(".betlist-radio-static .oddsArr").removeClass("odds").hide();
	// 		$("#empOdds").addClass("odds").show();
	// 	}
	// });

	$(".betselection-link").click (function () {
		var togglebet = $(this).find(".betarrow").css ("display");
		// var enablefunction = $(".betselection.betlistactive").length;
		// var limiter = ($(".betlist-radio.betlistactive-radio .odds").attr('value')) ? ($(".betlist-radio.betlistactive-radio .odds").attr('value')).split(',')[1] : 99;
		var b = $(this).find('.resultitem').html();
		var selectsingle = $(this).hasClass('betselection-link-single')

		if (togglebet == "none" && !$(this).hasClass('bet_disabled') ) {
			$(this).toggleClass( "betlistactive");
			$(this).find(".betarrow").css ("display" , "block");

			if(!selectsingle) { $('.betselection-link.lb-'+b).not('.betlistactive').addClass("bet_disabled"); }
			// if (enablefunction == limiter-1) {
			// 	$(".betselection").not(".betlistactive").addClass("bet_disabled");
			// }

		} else if (togglebet != "none" && !$(this).hasClass('bet_disabled')) {
			$(this).removeClass( "betlistactive");
			$(this).find(".betarrow").css ("display" , "none");
			if(!selectsingle)  { $('.betselection-link.lb-'+b).removeClass("bet_disabled"); }
		}
	
	});

	$(".betlist-radio").click (function () {
		$(".betarrow-radio").css ("display" , "none");
		$(".betlistactive-radio").removeClass("betlistactive-radio");
		
		$(".betselection").removeClass ("betlistactive");
		$(".betselection .betarrow").css ("display", "none");
		$(".bet_disabled").removeClass("bet_disabled");

		var togglebet = $(this).find(".betarrow-radio").css ("display");
		var enablefunction = $(this).find("p").html ();
		if (enablefunction != "--") {
			if (togglebet == "none") {
				$(this).toggleClass( "betlistactive-radio");
				$(this).find(".betarrow-radio").css ("display" , "block");
			} else {
				$(this).toggleClass( "betlistactive-radio");
				$(this).find(".betarrow-radio").css ("display" , "none");
			}
		}
	});
	
	// Normal submit tickets
	$(".submitticket").click (function () {
		if ($('.betlistactive').size() > 0) {
			var gameArray = [];
			//var betcontent = $(this).parent ().parent ().parent ().next (".bet_content");
			//var activebet = $(betcontent).children ().find(".betlistactive");
			var activebet = $(".betlistactive");
			var count = 0;
			$(activebet).each(function(){
				var gametitle = $(this).parents(".row").find(".rowtitle").html();
				var subgamename = $(this).find("h1").attr('title');
				var subgameodd = $(this).find("p").html();
				var subgameid = $(this).find(".betarrow").attr("rel");
				gameArray.push([gametitle, subgamename, subgameodd, subgameid]);
				count = count + 1;
			});
			resetMobileAll ();
			parent.displaysubmitbet (gameArray, 0);
		}
	});

	// Submit with Section + Choices
	$(".submit_selectionticket").click (function () {
		var min_limit = ($(".betlist-radio.betlistactive-radio .odds").attr('value')) ? $(".betlist-radio.betlistactive-radio .odds").attr('value').split(',')[0] : 0;
		var selection = $('.betlistactive').size();

		if (min_limit > 0  && selection >= min_limit ) {
			var gameArray = [];
			var contentArray = [];
			var activebet = $('.betselection.betlistactive').find('.betarrow');
			$(activebet).each(function(){
				contentArray.push($(this).attr('rel'));
			});
			var combination = LIBS.combination(selection, min_limit);
			var gametitle = $(".betlistactive-radio h1").html();
			var subgamename = contentArray.toString() + "<br/>" + "复式<br/>『 "+combination+" 组 』";
			var subgameodd = $(".betlistactive-radio").find(".odds").html();
			var subgameid = $(".betlistactive-radio").find(".odds").attr('ref');

			var mcount = min_limit;
			var multiple = combination;
			var state = ($(".betlist-radio.betlistactive-radio .odds").attr('state')) ? $(".betlist-radio.betlistactive-radio .odds").attr('state') : 0;
			var contents = contentArray.toString();

			gameArray.push([gametitle, subgamename, subgameodd, subgameid, mcount, multiple, state, contents]);

			$('.bet_panel').attr('combination',combination);
			resetMobileAll ();
			parent.displaysubmitbet (gameArray);
		}
	});

	// Submit multiple tickets
	$(".submit_selectionArrayticket").click (function () {
		var min_limit = ($(".betlist-radio-static .odds").attr('value')) ? $(".betlist-radio-static .odds").attr('value').split(',')[0] : 0;
		var max_limit = ($(".betlist-radio-static .odds").attr('value')) ? $(".betlist-radio-static .odds").attr('value').split(',')[1] : 0;
		var selection = $('.betlistactive').size();

		if (selection >= min_limit && selection <= max_limit && $('.odds').html() != "--") {
			var gameArray = [];
			var contentArray = [];
			var subgameArray = [];
			var activebet = $('.betselection.betlistactive').find('.betarrow');
			var activebet2 = $('.betselection.betlistactive').find('.balls');
			$(activebet).each(function(){
				contentArray.push($(this).attr('rel'));
			});
			$(activebet2).each(function(){
				subgameArray.push($(this).attr('data'));
			});
			var gametitle = $(".betlist-radio-static h1").html();
			var subgamename = subgameArray.toString();
			var subgameodd = $(".betlist-radio-static").find(".odds").html();
			var subgameid = $(".betlist-radio-static").find(".odds").attr('ref');

			var mcount = contentArray.length;
			var multiple = 1;
			var state = contentArray.length;
			var contents = contentArray.toString();

			gameArray.push([gametitle, subgamename, subgameodd, subgameid, mcount, multiple, state, contents]);

			$('.bet_panel').attr('combination',1);
			resetMobileAll ();
			parent.displaysubmitbet (gameArray);
		}
	});

	$(".submit_selectionAggregtateticket").click (function () {
		var min_limit = ($('.min_limit')) ? $('.min_limit').attr('min') : 0;
		var selection = $('.betlistactive').size();

		if (selection >= min_limit) {
			var gameArray = [];
			var contentArray = [];
			var subgameArray = [];

			var nums = [];

			var activebet = $('.betselection.betlistactive')
			var activebet2 = activebet.find('.betarrow')

			$(activebet2).each(function() {
				nums.push($(this).attr('rel'));
			});
			
			nums = nums.sort();
			nums = LIBS.comboArray(nums, min_limit);

			$.each(nums, function( k, v ) {
				var gametitle = $(".betlist-radio-static h1").html();
				var subgamename = [];
				var todds = 0;
				$.each(v, function( k1, v1 ) {
					subgamename.push($('.balls.selection_'+v1).attr('data'));
					odds = parseFloat($('.odds.selection_'+v1).html());
					
					if (todds == 0) { 
						todds = odds 
					} else if (MULTIPLE[4]) {
						if (odds < todds) {
							todds = odds;
						}
					} else {
						if (odds > todds) {
							todds = odds;
						}
					}

				});
				var subgamename = subgamename.toString();
				var subgameodd = todds;
				var subgameid = MULTIPLE[1];
				var contentArray = v;

				var mcount = contentArray.length;
				var multiple = 1;
				var state = 0;
				var contents = contentArray.toString();
				gameArray.push([gametitle, subgamename, subgameodd, subgameid, mcount, multiple, state, contents]);
			});
			// // $(activebet2).each(function(){
			// // 	subgameArray.push($(this).attr('data'));
			// // });
			// // var subgamename = subgameArray.toString();
			// // var subgameodd = $(".betlist-radio-static").find(".odds").html();
			// // var subgameid = $(".betlist-radio-static").find(".odds").attr('ref');

			// var mcount = contentArray.length;
			// var multiple = 1;
			// var state = 0;
			// var contents = contentArray.toString();

			// gameArray.push([gametitle, subgamename, subgameodd, subgameid, mcount, multiple, state, contents]);

			$('.bet_panel').attr('combination',1);
			resetMobileAll ();
			parent.displaysubmitbet (gameArray);
		} else {
			alert('请至少选择'+min_limit+'个选择')
		}
	});
	
	// Submit multiple tickets, linked choices, disable one
	$(".submit_linkticket").click (function () {
		var lrowcount = true;
		var nums = [];

		$('.lrow').each(function () {
			var betlist;
			betlist = $(this).find('.betlistactive');

			if (betlist.length == 0) {
				lrowcount = false;
			} else {
				var items = [];
				betlist.each(function() {
					items.push($(this).find('.resultitem').html());		
				});
				items.sort();
				nums.push(items);
			}
		});
		
		if (lrowcount && nums.length > 0 && $(".betlist-radio-static").find(".odds").html() != "--") {
			var totalCombo = LIBS.comboList(nums);
			var gameArray = [];
			var contentArray = [];

			// var activebet = $('.betselection.betlistactive').find('.betarrow');
			// $(activebet).each(function(){
			// 	contentArray.push($(this).attr('rel'));
			// });
			// var combination = LIBS.combination(selection, min_limit);

			$.each(totalCombo, function( k, v ) {
				var gametitle = $(".betlist-radio-static h1").html();
				var subgamename = v;
				var subgameodd = $(".betlist-radio-static").find(".odds").html();
				var subgameid = $(".betlist-radio-static").find(".odds").attr('ref');

				var mcount = null;
				var multiple = 1;
				var state = "0";
				var contents = v;

				gameArray.push([gametitle, subgamename, subgameodd, subgameid, mcount, multiple, state, contents]);
			});

			resetMobileAll ();
			parent.displaysubmitbet (gameArray);
		}
	});

});